import json
from ast import literal_eval

from django.core import serializers
from django.db.models import Count
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

from backend.models import StudioInfo, StudioMember


# 查询工作室分组接口
@csrf_exempt
def studio_group(request):
    response = {}
    try:
        info = StudioInfo.objects.values('studio_name').annotate(uid=Count(id))
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
        response['list'] = json.dumps(list(info))
    except Exception as e:
        response['respCode'] = '999999'
        response['respMsg'] = str(e)
    return JsonResponse(response)


# 新增工作室接口
@csrf_exempt
def studio_add(request):
    response = {}
    try:
        form = json.loads(request.GET.get('form'))

        StudioInfo.objects.create(studio_name=form['studio_name'], studio_img=form['studio_img'],
                                  studio_courseTable=form['studio_courseTable']
                                  , studio_location=form['studio_location'], studio_position=form['studio_position']
                                  , studio_openTime=form['studio_openTime'])
        response['list'] = form
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respCode'] = '999999'
        response['respMsg'] = str(e)
    return JsonResponse(response)


# 查询工作室接口
@csrf_exempt
def studio_info(request):
    response = {}
    try:
        if request.GET.get('topForm'):
            top_form = json.loads(request.GET.get('topForm'))
        else:
            top_form = None
        cid = request.GET.get('id')
        if top_form:
            if top_form['studio_name']:
                if top_form['studio_location']:
                    info = StudioInfo.objects.filter(studio_location=top_form['studio_location'],
                                                     studio_name=top_form['studio_name'])
                else:
                    info = StudioInfo.objects.filter(studio_name=top_form['studio_name'])
            elif top_form['studio_location']:
                info = StudioInfo.objects.filter(studio_location=top_form['studio_location'])
            else:
                info = StudioInfo.objects.all()
        elif cid:
            info = StudioInfo.objects.filter(id=cid)
        else:
            info = StudioInfo.objects.filter().all()
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
        response['list'] = list(info.values())
    except Exception as e:
        response['respCode'] = '999999'
        response['respMsg'] = str(e)
    return JsonResponse(response)


# 编辑工作室接口
@csrf_exempt
def studio_edit(request):
    response = {}
    try:
        form = json.loads(request.GET.get('form'))
        StudioInfo.objects.filter(id=form['id']).update(studio_name=form['studio_name'], studio_img=form['studio_img'],
                                                        studio_courseTable=form['studio_courseTable']
                                                        , studio_location=form['studio_location'],
                                                        studio_position=form['studio_position']
                                                        , studio_openTime=form['studio_openTime'])
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respCode'] = '999999'
        response['respMsg'] = str(e)
    return JsonResponse(response)


# 删除工作室接口
@csrf_exempt
def studio_del(request):
    response = {}
    try:
        cid = request.GET.get('id')
        StudioInfo.objects.filter(id=cid).delete()
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respCode'] = '999999'
        response['respMsg'] = str(e)
    return JsonResponse(response)


# 工作室成员信息
@csrf_exempt
def studio_members_info(request):
    response = {}
    try:
        if request.GET.get('topForm'):
            top_form = json.loads(request.GET.get('topForm'))
        else:
            top_form = None
        belong_studio = request.GET.get('studioName')
        cid = request.GET.get('id')
        if top_form:
            if top_form['studio_member']:
                if top_form['studio_belong']:
                    info = StudioMember.objects.filter(studio_member=top_form['studio_member'],
                                                       studio_belong=top_form['studio_belong'])
                else:
                    info = StudioMember.objects.filter(studio_member=top_form['studio_member'])
            elif top_form['studio_belong']:
                info = StudioMember.objects.filter(studio_member=top_form['studio_belong'])
            else:
                info = StudioMember.objects.filter().all()
        elif cid:
            info = StudioMember.objects.filter(id=cid)
        elif belong_studio:
            info = StudioMember.objects.filter(studio_belong=belong_studio)
        else:
            info = StudioMember.objects.filter().all()
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
        response['list'] = list(info.values())
    except Exception as e:
        response['respCode'] = '999999'
        response['respMsg'] = str(e)
    return JsonResponse(response)


# 新增工作室成员接口
@csrf_exempt
def studio_members_add(request):
    response = {}
    try:
        form_item = json.loads(request.body)
        form = literal_eval(form_item['params']['form'])
        StudioMember.objects.create(studio_member=form['studio_member'],
                                    studio_memberImg=form['studio_memberImg'],
                                    studio_memberDesc=form['studio_memberDesc'],
                                    studio_belong=form['studio_belong'])
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respCode'] = '999999'
        response['respMsg'] = str(e)
    return JsonResponse(response)


# 删除工作室成员接口
@csrf_exempt
def studio_members_del(request):
    response = {}
    try:
        cid = request.GET.get('id')
        StudioMember.objects.filter(id=cid).delete()
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respCode'] = '999999'
        response['respMsg'] = str(e)
    return JsonResponse(response)


# 编辑工作室成员接口
@csrf_exempt
def studio_members_edit(request):
    response = {}
    try:
        form = json.loads(request.body)
        StudioMember.objects.filter(id=form['id']).update(studio_member=form['studio_member'],
                                                          studio_memberImg=form['studio_memberImg'],
                                                          studio_memberDesc=form['studio_memberDesc'],
                                                          studio_belong=form['studio_belong'])
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respCode'] = '999999'
        response['respMsg'] = str(e)
    return JsonResponse(response)
